package com.example.demo.controller;
import com.example.demo.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller//将它交给spring管理
@ResponseBody//返回非页面数据
@RequestMapping("/user") //一级路由映射
public class UserController {

   //get请求三种写法
    @RequestMapping("/springMVC1")//二级路由映射(默认是get请求)
    public String saySpringMVC(){

        return "springMVC1";
    }
    @GetMapping("/springMVC2")
    public String saySpringMVC2(){

        return "springMVC2";
    }
    @RequestMapping(value = "/springMVC3",method = RequestMethod.GET)//二级路由映射
    public String saySpringMVC3(){

        return "springMVC3";
    }

    //post请求的两种写法
    //写法一：
    @PostMapping("/hi")
    public String sayHai(){
        return "hi1";
    }
    //写法二：
    @RequestMapping(value = "/hi2",method = RequestMethod.POST)
    public String sayHi2(){
        return "hi2";
    }

    //获取参数
    @GetMapping("/getPar")
    public Object getPar(String name){

        System.out.println("name" + name);

        return name;
    }

    //传递对象
    @GetMapping("/getPerson")
     public Person getPerson(Person person){

         System.out.println("名字：" + person.getName());
         System.out.println("年龄：" + person.getGender());
         System.out.println("性别: "+ person.getGender());

         return person;
     }

     //后端参数重命名
    //后端参数重命名，前端传参url用time,后端参数重命名为createTime
    @GetMapping("/reName")
    public Object reName(@RequestParam("time") String createTime){

        System.out.println("时间：" + createTime);

        return createTime;
    }

    //设置参数为非必传
    //value = "time",required = false 代表前端参数为time，非必传，可传可不传，程序都不会报错
    @GetMapping("/method1")
    public Object method1(@RequestParam(value = "time",required = false) String createTime){
        System.out.println("时间：" + createTime);
        return createTime;
    }

    //获取url中的参数
    @PostMapping("/param/{name}/{age}")
    public Object method2(@PathVariable String name,@PathVariable String age){
        System.out.println("name" +name);
        System.out.println("age" + age);
        return "name :" + name + " age: " + age;
    }

    //获取request和response对象
     @GetMapping("/method3")
    public String method3(HttpServletRequest request, HttpServletResponse response){

        String name=request.getParameter("name");
         Cookie[] cookie=request.getCookies();
         return name+" 你好";
    }

    //获取cookie
    @GetMapping("/method4")
    public String method4(@CookieValue("cookie") String cookie){

        return "cookie" + cookie;
    }

    //session存储
    @GetMapping("/sess")
    public String sess(HttpServletRequest request){
        //置为true，如果session不存在，自动创建
        HttpSession session=request.getSession(true);
        if(session != null ){
            session.setAttribute("name","张三");
        }
        return "存储成功";
    }


    //session获取,从HttpServletRequest中获取
   @GetMapping("/session")
    public String session(HttpServletRequest request){
        //置为false，如果session不存在，不会自动创建
       HttpSession session=request.getSession(false);
       String username="";
       if(session != null || session.getAttribute(username) != null){

           username=(String)session.getAttribute("username");
       }
       return "username" +username;
    }

}
